package com.hyts.standard.webflux.web.mvc.user;

import com.hyts.standard.webflux.base.handler.AbstractCrudDataHandler;
import com.hyts.standard.webflux.bean.dto.UserDTO;
import com.hyts.standard.webflux.bean.po.UserPO;
import com.hyts.standard.webflux.r2db.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @project-name:standard-webflux
 * @package-name:com.hyts.standard.webflux.handler
 * @author:Libo/Alex
 * @create-date:2019-06-19 10:07
 * @email:liboware@gmail.com
 * @description:此类主要用于: 基础handler控制器处理器
 */
@Component
public class UserDataHandler extends AbstractCrudDataHandler {


    @Autowired
    UsersService usersService;


    @Override
    public Mono<ServerResponse> save(ServerRequest serverRequest) {
        usersService.save(serverRequest.queryParam("userName").get());
        return restPost(serverRequest);
    }


    @Override
    public Mono<ServerResponse> remove(ServerRequest serverRequest) {
        return restGet(serverRequest);
    }


    @Override
    public Mono<ServerResponse> load(ServerRequest serverRequest) {
        return restGet(serverRequest);
    }


    /**
     * search操作处理
     * @param serverRequest
     * @return
     */
    @Override
    public Mono<ServerResponse> search(ServerRequest serverRequest) {
        Flux<UserPO> userPOFlux = usersService.search(serverRequest.queryParam("userName").get());
        userPOFlux.next().subscribe(System.out::println);
        return restGet(serverRequest);
    }
}
